import os
import pandas as pd
from pathlib import Path
import chardet


def detect_encoding(file_path, sample_size=1024):
    """自动检测文件编码"""
    with open(file_path, "rb") as f:
        raw_data = f.read(sample_size)
    result = chardet.detect(raw_data)
    return result["encoding"].lower()


def convert_csv_encoding(source_dir, target_dir, force_convert=False):
    """
    智能批量转换CSV文件编码（非UTF-8 → UTF-8）
    :param source_dir: 源目录路径
    :param target_dir: 目标目录路径
    :param force_convert: 是否强制转换所有文件（默认False）
    """
    # 创建目标目录
    Path(target_dir).mkdir(parents=True, exist_ok=True)

    # 支持的编码列表（可扩展）
    chinese_encodings = ["gb2312", "gbk", "gb18030", "big5"]

    for filename in os.listdir(source_dir):
        source_path = os.path.join(source_dir, filename)
        try:
            # 先尝试UTF-8
            try:
                pd.read_csv(source_path, encoding="utf-8")
                print(f"跳过 [已是UTF-8]: {filename}")
                continue
            except UnicodeDecodeError:
                # 回退到中文编码
                try:
                    df = pd.read_csv(source_path, encoding="gb18030")  # 最全的中文编码
                except UnicodeDecodeError:
                    print(f"警告：无法解码中文文件 {filename}，尝试忽略错误...")
                    df = pd.read_csv(source_path, encoding="gb18030", errors="replace")

            target_path = os.path.join(target_dir, filename)
            df.to_csv(target_path, index=False, encoding="utf-8")
            print(f"转换成功: {filename}")
        except Exception as e:
            print(f"严重错误: {filename} - {str(e)}")


# 使用示例
if __name__ == "__main__":
    source_directory = "csv-files"  # 源目录
    target_directory = "output_utf8"  # 目标目录

    # 默认只转换非UTF-8的中文编码文件
    convert_csv_encoding(source_directory, target_directory)
